﻿namespace MicroCloud.Utils
{
    /// <summary>
    /// 数组扩展方法
    /// </summary>
    public static class ArrayExtensions
    {
        /// <summary>
        /// 复制一份二维数组的副本
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static byte[,] Copy(this byte[,] bytes)
        {
            int width = bytes.GetLength(0), height = bytes.GetLength(1);
            byte[,] newBytes = new byte[width, height];
            Array.Copy(bytes, newBytes, bytes.Length);
            return newBytes;
        }

        /// <summary>
        /// 向数组中添加项
        /// </summary>
        /// <param name="array">源数组</param>
        /// <param name="item">要添加的项</param>
        /// <returns></returns>
        public static T[] Add<T>(this T[] array, T item)
        {
            array ??= Array.Empty<T>();

            // 创建新数组并添加元素
            T[] newArray = new T[array.Length + 1];
            array.CopyTo(newArray, 0);
            newArray[^1] = item;

            return newArray;
        }

    }

}
